/*
 * @Author       : 邱狮杰
 * @Date         : 2021-05-18 14:14:12
 * @LastEditTime : 2021-05-18 16:28:26
 * @FilePath     : /wedding-dress-selection/src/reducer/category.tsx
 * @Description  : 品类
 */
import { useReducer, createContext, Dispatch } from 'react'

export const CreateCategoryContext = createContext({})
const { Provider } = CreateCategoryContext

export interface CategoryStateConfig {
  id: null | number
}
export const initState: CategoryStateConfig = {
  id: null,
}
// 获取品类所有信息
export const GETDETAILES = 'getDetails'
// 修改品类信息
export const RESET = 'reset'

export type CategoryAction = {
  type: typeof GETDETAILES | typeof RESET
  data: CategoryStateConfig
}
export interface useContextCategoryContextType {
  CategoryState: CategoryStateConfig
  CategoryDispatch: Dispatch<CategoryAction>
}

export function CategoryReducer(state: CategoryStateConfig, action: CategoryAction): CategoryStateConfig {
  switch (action.type) {
    case GETDETAILES:
      return state
    case RESET:
      return action.data
    default:
      return state
  }
}

export const CategoryContext = (props: any) => {
  const [CategoryState, CategoryDispatch] = useReducer(CategoryReducer, initState)
  return <Provider value={{ CategoryState, CategoryDispatch }}>{props.children}</Provider>
}
